<template>
	<view class="flex-col page">
		<view class="flex-col group">
			<view class="flex-col section_1">
				<!-- #ifdef APP-PLUS -->
				<view class="status_bar">
					<view class="top_view"></view>
				</view>
				<!-- #endif -->
				<view class="flex-col group_1">
					<view class="flex-row group_2">
						<text @click.stop="navigateTo('/pages/classification/index?from=bank', false)">
							{{ cateName || '请选择分类' }}
						</text>
						<image src="/static/local/be16c30d7a4f4e3128756ccf42bfd428.png" class="image_1" />
					</view>
					<view class="flex-row equal-division">
						<view class="flex-col equal-division-item">
							<view class="text_4">
								<u-count-to :start-val="0" :duration="1200" :end-val="rightVal"></u-count-to>
								<text class="text_2">%</text>
							</view>
							<text class="text_5">综合正确率</text>
						</view>
						<view class="flex-col equal-division-item">
							<u-count-to :start-val="0" :duration="1200" :end-val="todayNum" class="text_4"></u-count-to>
							<text class="text_5">今日做题</text>
						</view>
						<view class="flex-col equal-division-item">
							<u-count-to :start-val="0" :duration="1200" :end-val="totalNum" class="text_4"></u-count-to>
							<text class="text_5">累计做题</text>
						</view>
					</view>
				</view>
			</view>
			<view class="flex-col group_4">
				<view class="grid">
					<view class="grid-item flex-row" @tap="jumpPage('everyday')">
						<view class="flex-col group_5">
							<text class="text_8">每日一练</text>
							<text class="text_10">每天练练，稳步提升</text>
						</view>
						<image src="/static/local/16403089502735192479.png" class="image_2" />
					</view>
					<view class="grid-item flex-row" @click="jumpPage('chapter')">
						<view class="flex-col group_6">
							<text class="text_8">章节练习</text>
							<text class="text_10">针对训练，重点掌握</text>
						</view>
						<image src="/static/local/16403089825739296713.png" class="image_3" />
					</view>
					<view class="grid-item flex-row" @click="jumpPage('exam')">
						<view class="flex-col group_7">
							<text class="text_8">历年真题</text>
							<text class="text_10">紧扣大纲，梳理考点</text>
						</view>
						<image src="/static/local/16403089825749858373.png" class="image_4" />
					</view>
					<view class="grid-item flex-row" @click="jumpPage('simulation')">
						<view class="flex-col group_8">
							<text class="text_8">模拟试题</text>
							<text class="text_10">模拟试卷，试题精选</text>
						</view>
						<image src="/static/local/16403089825745633097.png" class="image_5" />
					</view>
				</view>
				<view class="flex-row equal-division_1">
					<view class="flex-col items-center equal-division-item_3" @tap="jumpToVip()">
						<image src="/static/local/16403089825741983586.png" class="image_6" />
						<text class="text_18">会员专享</text>
					</view>
					<view class="flex-col items-center equal-division-item_4" @tap="jumpToError()">
						<image src="/static/local/16403089825754414817.png" class="image_7" />
						<text class="text_19">错题集</text>
					</view>
					<view class="flex-col items-center equal-division-item_5" @tap="jumpToMyCollection()">
						<image src="/static/local/879dc778630b12f70e93b25aee16735f.png" class="image_8" />
						<text class="text_20">我的收藏</text>
					</view>
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	var that; // 当前页面对象
	var vk; // vk依赖
	export default {
		data() {
			return {
				// 当前分类信息
				cateId: '',
				cateName: '',
				// 题目信息
				totalNum: 0,
				todayNum: 0,
				rightVal: 0
			}
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.init(options)
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {
			let bankCate = vk.getStorageSync('bank_cate')
			if (bankCate) {
				this.cateId = bankCate.id
				this.cateName = bankCate.name
			}
			
			that.totalNum = 0
			that.todayNum = 0
			that.rightVal = 0
			
			that.getInfo()
		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options) {
				uni.$on('bankUpdate', function(data) {})
			},
			getInfo() {
				if (!uni.vk.callFunctionUtil.checkToken()) return
				let cateInfo = vk.getStorageSync('bank_cate');

				vk.callFunction({
					url: 'client/quest-bank/kh/getMyInfo',
					data: {
						need_user_info: true,
						cate_id: cateInfo ? cateInfo.id : null
					},
					success: function(res) {
						that.totalNum = res.data.length
						let right = 0
						let { todayStart, todayEnd } = vk.pubfn.getCommonTime(new Date())
						res.data.map(v => {
							if (v.is_right == 1) right++
							if (v._add_time > todayStart && v._add_time < todayEnd) that.todayNum++
						})
						if (that.totalNum == 0) return that.rightVal = 0
						if (right == 0) return that.rightVal = 0
						that.rightVal = Number((right / that.totalNum * 100).toFixed(0))
					}
				})
			},
			// 检测是否存在分类
			jumpPage(url) {
				if (this.checkLogin()) return
				if (this.cateName == '') {
					this.$refs.uToast.show({
						title: '请选择分类',
						type: 'warning',
						duration: 1000
					})
					return
				}
				switch (url) {
					case 'chapter':
						uni.navigateTo({
							url: `/pages/question-bank/chapter-test?cateName=${this.cateName}&cateId=${this.cateId}`
						});
						return
					case 'simulation':
						uni.navigateTo({
							url: `/pages/question-bank/simulation?cateName=${this.cateName}&cateId=${this.cateId}`
						});
						return
					case 'exam':
						uni.navigateTo({
							url: `/pages/question-bank/real-exam?cateName=${this.cateName}&cateId=${this.cateId}`
						});
						return
					case 'everyday':
						uni.navigateTo({
							url: `/pages/question-bank/everyday-practice?cateName=${this.cateName}&cateId=${this.cateId}`
						});
						return
				}
			},
			// 跳转我的收藏
			jumpToMyCollection() {
				if (this.checkLogin()) return
				uni.navigateTo({
					url: '/pages/question-bank/question-collection'
				});
			},
			// 跳转错题集
			jumpToError() {
				if (this.checkLogin()) return
				uni.navigateTo({
					url: '/pages/question-bank/error-question'
				});
			},
			// 跳转会员专享
			jumpToVip() {
				if (this.checkLogin()) return
				uni.navigateTo({
					url: `/pages/question-bank/vip?&cateId=${this.cateId}`
				});
			},
			// 验证登录
			checkLogin() {
				if (!uni.vk.callFunctionUtil.checkToken()) {
					vk.toast('请先前往登录', 'none')
					setTimeout(() => { vk.navigateTo('/pages/login/index/index') }, 600)
					return true
				}
			}
		},
		onUnload() {
			uni.$off('bankUpdate')
		}
	}
</script>
<style lang="scss" scoped>
	.page {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - 102rpx);
		/* #endif */
		background: #F5F5F5;
	}

	.group {
		flex: 1 1 auto;
		overflow-y: auto;

		.section_1 {
			padding-bottom: 142rpx;
			background-image: url('/static/local/16403089590017478445.png');
			background-position: 0px 0px;
			background-size: 100% 100%;
			background-repeat: no-repeat;

			.group_1 {
				margin-top: 24rpx;
				padding: 0 11rpx;
				height: 226rpx;

				.group_2 {
					padding: 0 9rpx;
					color: rgb(51, 51, 51);
					font-size: 34rpx;
					line-height: 41rpx;
					white-space: nowrap;

					.image_1 {
						margin: 24rpx 0 0 6rpx;
						width: 12rpx;
						height: 12rpx;
					}
				}

				.equal-division-item {
					margin-top: 60rpx;
					padding: 10rpx 21rpx;
					flex: 1 1 242rpx;
					justify-content: center;

					.text_4 {
						text-align: center;
						color: rgb(51, 51, 51);
						font-size: 46rpx;
						white-space: nowrap;
						font-weight: 600;
						display: inline-block !important;

						.text_2 {
							font-size: 28rpx;
							font-weight: 400;
							margin-left: 6rpx;
						}
					}

					.text_5 {
						margin-top: 29rpx;
						align-self: center;
						color: rgb(51, 51, 51);
						font-size: 24rpx;
						line-height: 29rpx;
						white-space: nowrap;
					}
				}
			}
		}
	}

	.group_4 {
		margin-top: -88rpx;
		padding: 0 20rpx;
		position: relative;

		.grid {
			height: 348rpx;
			display: grid;
			grid-template-columns: repeat(2, 1fr);
			grid-row-gap: 26rpx;
			grid-column-gap: 24rpx;

			.grid-item {
				padding: 40rpx 20rpx 38rpx;
				background-color: rgb(255, 255, 255);
				border-radius: 12rpx;

				.group_5 {
					flex: 1 1 auto;
				}

				.image_2 {
					margin-left: 19rpx;
					margin-right: 3rpx;
					align-self: center;
					width: 61rpx;
					height: 63rpx;
				}

				.group_6 {
					flex: 1 1 auto;
				}

				.image_3 {
					margin: 8rpx 0 10rpx 17rpx;
					width: 64rpx;
					height: 66rpx;
				}

				.group_7 {
					flex: 1 1 auto;
				}

				.image_4 {
					margin-left: 22rpx;
					margin-right: 7rpx;
					align-self: center;
					width: 55rpx;
					height: 64rpx;
				}

				.group_8 {
					flex: 1 1 auto;
				}

				.image_5 {
					margin-left: 18rpx;
					margin-right: 3rpx;
					align-self: center;
					width: 62rpx;
					height: 54rpx;
				}

				.text_8 {
					color: rgb(51, 51, 51);
					font-size: 30rpx;
					line-height: 36rpx;
					white-space: nowrap;
				}

				.text_10 {
					margin-top: 20rpx;
					color: rgb(153, 153, 153);
					font-size: 24rpx;
					line-height: 29rpx;
					white-space: nowrap;
				}
			}
		}

		.equal-division_1 {
			margin-top: 24rpx;
			padding: 29rpx 0 26rpx;
			color: rgb(51, 51, 51);
			font-size: 28rpx;
			line-height: 34rpx;
			white-space: nowrap;
			background-color: rgb(255, 255, 255);
			border-radius: 12rpx;

			.equal-division-item_3 {
				padding: 14rpx 0 11rpx;
				flex: 1 1 237rpx;

				.image_6 {
					width: 62rpx;
					height: 51rpx;
				}

				.text_18 {
					margin-top: 29rpx;
				}
			}

			.equal-division-item_4 {
				padding: 12rpx 0 11rpx;
				flex: 1 1 237rpx;

				.image_7 {
					width: 55rpx;
					height: 55rpx;
				}

				.text_19 {
					margin-top: 27rpx;
				}
			}

			.equal-division-item_5 {
				padding: 9rpx 0 11rpx;
				flex: 1 1 237rpx;

				.image_8 {
					width: 61rpx;
					height: 61rpx;
				}

				.text_20 {
					margin-top: 25rpx;
				}
			}
		}
	}
</style>
